{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     fun: 10.651091840619724\n",
      "     jac: array([1.10433471, 2.40651846, 1.89564812])\n",
      " message: 'Optimization terminated successfully.'\n",
      "    nfev: 177\n",
      "     nit: 30\n",
      "    njev: 30\n",
      "  status: 0\n",
      " success: True\n",
      "       x: array([0.55216734, 1.20325918, 0.94782404])\n",
      "10.651091840619724\n"
     ]
    }
   ],
   "source": [
    "import scipy.optimize\n",
    "import numpy as np\n",
    "\n",
    "# 'eq'表示表达式等于0，'ineq'表示表达式大于等于0\n",
    "f = lambda x: x[0]**2 + x[1]**2 + x[2]**2 + 8\n",
    "constraints = [{'type': 'ineq', 'fun': lambda x: x[0]**2 - x[1] + x[2]**2},\n",
    "              {'type': 'ineq', 'fun': lambda x: 20 - x[0] - x[1]**2 - x[2]**3},\n",
    "              {'type': 'eq', 'fun': lambda x: -x[0] - x[1]**2 +2},\n",
    "              {'type': 'eq', 'fun': lambda x: x[1] + 2*x[2]**2 - 3}]\n",
    "bounds = [(0, None), (0, None), (0, None)]\n",
    "\n",
    "#设置初始猜测值\n",
    "x0 = np.asarray((0.5,0.5,0.5))\n",
    "\n",
    "res = scipy.optimize.minimize(f, x0, method='SLSQP',constraints=constraints)\n",
    "print(res)\n",
    "print(f(res.x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     fun: 6.09154857268997e-17\n",
      "     jac: array([-4.44089210e-16,  1.74802086e-08,  1.10325904e-08,  0.00000000e+00,\n",
      "        0.00000000e+00])\n",
      " message: 'Optimization terminated successfully.'\n",
      "    nfev: 15\n",
      "     nit: 2\n",
      "    njev: 2\n",
      "  status: 0\n",
      " success: True\n",
      "       x: array([0.49999999, 0.5       , 0.5       , 1.        , 1.        ])\n",
      "6.09154857268997e-17\n",
      "-2.959690048602943\n",
      "-2.9596900398628385\n"
     ]
    }
   ],
   "source": [
    "import scipy.optimize\n",
    "import numpy as np\n",
    "\n",
    "# 'eq'表示表达式等于0，'ineq'表示表达式大于等于0\n",
    "f = lambda x: (x[0]-0.5)**2 + (x[1]-0.5)**2 + (x[2]-0.5)**2\n",
    "K1 = lambda x: np.sin(x[3]*x[0]) * np.cos(x[3]*x[1]) - (x[3]-50)**2/1000 - np.sin(x[3]*x[2]) - x[2]\n",
    "K2 = lambda x: np.sin(x[4]*x[1]) * np.cos(x[4]*x[0]) - (x[4]-50)**2/1000 - np.sin(x[4]*x[2]) - x[2]\n",
    "constraints = [{'type': 'ineq', 'fun': lambda x: 1 - K1(x)},\n",
    "               {'type': 'ineq', 'fun': lambda x: 1 - K2(x)}\n",
    "              ]\n",
    "bounds = [(None, None), (None, None), (None, None), (1, 100), (1, 100)]\n",
    "\n",
    "#设置初始猜测值\n",
    "x0 = np.asarray((0.5, 0.5, 0.5, 0.5, 0.5))\n",
    "x0 = np.array([0.5, 0.2, 0.3, 0, 0])\n",
    "\n",
    "res = scipy.optimize.minimize(f, x0, method='SLSQP', bounds = bounds, constraints=constraints)\n",
    "print(res)\n",
    "print(f(res.x))\n",
    "print(K1(res.x))\n",
    "print(K2(res.x))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
